<?php
  include_once('staff.php');
  $where = '';
  $name = $_REQUEST['name'];
  $author = $_REQUEST['author'];
  $num = $_REQUEST['num'];

  function decode($var) {
	$var = iconv('UTF-8','CP1251',$var);
	return $var;
  }
  
  function setquery($fieldv,$fieldn) {
  global $where;
	if ($fieldv!='') {
		$where =$where. ' AND '.$fieldn." like '%".$fieldv."%'";
	}
  }
 
 $name = decode($name);
 $author = decode($author);
 setquery($name,'title');
 setquery($author,'author');
 setquery($num,'num');
 sql($where);
 
  function sql($where) {
	if ($where == '')
		$sql = "Select '' ";
	else
		$sql = "SELECT * from elib WHERE id > 0 ".$where;
	//echo $sql;
	$db = connectdb();
	$res = mysql_query($sql,$db);
	$subj = array();
	if(mysql_num_rows($res) > 0){
		while($row = mysql_fetch_assoc($res)){
			$row['title']=iconv('CP1251','UTF-8',$row['title']);
			$list[] = $row['title'];
			$list[][]=iconv('CP1251','UTF-8',$row['author']);
			$list[][][]=$row['num'];
			$list[][][][]=iconv('CP1251','UTF-8',$row['name']);
			$list[][][][][]=$row['spec_id'];
			$list[][][][][][]=$row['id'];
		} 
	}  else {
		$list = '';
	}
	result($list);
  }
  
 function result ($lala) { 
	$tmp = json_encode($lala);
	echo $tmp;
 }
?>